import { ProTable, type ProColumns } from '@/components/Pro';
import { lcControllerGetLcPageDatas } from '@/services/ess/deviceLc';
import { DATABASE_TYPE_ENUM, fetchDictInfo } from '@/utils';
import { useModel } from '@@/exports';
import dayjs from 'dayjs';
import React, { useEffect, useState } from 'react';
import { useIntl } from 'umi';

const HistoryData: React.FC = () => {
  const { initialState } = useModel('@@initialState');
  const intl = useIntl();

  const [powerOptions, setPowerOptions] = useState([]);
  // 总LC表格
  const columns: ProColumns<any>[] = [
    {
      title: intl.formatMessage({ id: 'data.s/N' }),
      dataIndex: 'pageIndex',
      hideInSearch: true,
      fixed: 'left',
      width: 60,
    },
    {
      title: intl.formatMessage({ id: 'systemManagement.neddTochange.lcType' }),
      dataIndex: 'lcType',
      valueType: 'select',
      initialValue: '1',
      valueEnum: {
        1: {
          text: intl.formatMessage({ id: 'systemManagement.neddTochange.totalLC' }),
        },
        2: {
          text: intl.formatMessage({ id: 'systemManagement.neddTochange.subLC' }),
        },
      },
      hideInTable: true,
    },
    {
      title: intl.formatMessage({ id: 'data.acquisitionTime' }),
      dataIndex: 'collectTime',
      key: 'collectTime',
      valueType: 'dateTimeRange',
      initialValue: [dayjs().startOf('day'), dayjs()],
      fixed: 'left',
      width: 100,
      render: (_, record) => {
        return <span>{record.collectTime}</span>;
      },
    },
    {
      title: intl.formatMessage({ id: 'data.reportingTime' }),
      dataIndex: 'reportTime',
      key: 'reportTime',
      valueType: 'dateTimeRange',
      initialValue: [dayjs().startOf('day'), dayjs()],

      fixed: 'left',
      width: 100,
      render: (_, record) => {
        return <span>{record.reportTime}</span>;
      },
    },
    {
      title: intl.formatMessage({ id: 'data.deviceType' }),
      dataIndex: 'product',
      key: 'product',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.deviceFactory' }),
      dataIndex: 'producer',
      key: 'producer',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.deviceID' }),
      dataIndex: 'devId',
      hideInSearch: true,
      width: 90,
    },
    {
      title: intl.formatMessage({ id: 'pcsMonitoring.eventStatus' }),
      dataIndex: 'sysRunState',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.gcbStatus' }),
      dataIndex: 'mainBreakerState',
      hideInSearch: true,
      width: 130,
      render: (_, record) => {
        return (
          <span>
            {record.mainBreakerState === 0
              ? intl.formatMessage({ id: 'data.disconnect' })
              : intl.formatMessage({ id: 'data.close' })}
          </span>
        );
      },
    },
    {
      title: intl.formatMessage({ id: 'data.gcbFault' }),
      dataIndex: 'mainBreakerFault',
      hideInSearch: true,
      width: 140,
      render: (_, record) => {
        return (
          <span>
            {record.mainBreakerFault === 1
              ? intl.formatMessage({ id: 'stationRealtimeMonitoring.error' })
              : intl.formatMessage({ id: 'stationRealtimeMonitoring.normal' })}
          </span>
        );
      },
    },
    {
      title: intl.formatMessage({ id: 'data.lcLineStatus' }),
      dataIndex: 'lcOnlineState',
      hideInSearch: true,
      width: 140,
      render: (_, record) => {
        return (
          <span>
            {record.lcOnlineState === 1
              ? intl.formatMessage({ id: 'bigdata.online' })
              : intl.formatMessage({ id: 'bigdata.offline' })}
          </span>
        );
      },
    },
    {
      title: intl.formatMessage({ id: 'data.lcWorkStatus' }),
      dataIndex: 'lcRunState',
      hideInSearch: true,
      width: 140,
    },
    {
      title: intl.formatMessage({ id: 'data.lcFault' }),
      dataIndex: 'lcFaultState',
      hideInSearch: true,
      width: 140,
    },
    {
      title: intl.formatMessage({ id: 'data.chargeDischargeStatus' }),
      dataIndex: 'chargeState',
      hideInSearch: true,
      width: 120,
    },
    {
      title: intl.formatMessage({ id: 'data.activePowerKw' }),
      dataIndex: 'activePower',
      hideInSearch: true,
      width: 130,
    },
    {
      title: intl.formatMessage({ id: 'data.maximumPower' }),
      dataIndex: 'ableMaxChgPower',
      hideInSearch: true,
      width: 170,
    },
    {
      title: intl.formatMessage({ id: 'data.maximumDischarge' }),
      dataIndex: 'ableMaxDchgPower',
      hideInSearch: true,
      width: 170,
    },
    {
      title: intl.formatMessage({ id: 'data.powerMode' }),
      dataIndex: 'powerMode',
      hideInSearch: true,
      width: 100,
      render: (_, record) => {
        const text = powerOptions.find((item) => {
          return Number(item.value) === Number(record?.powerMode);
        })?.label;
        return <span>{text}</span>;
      },
    },
    {
      title: intl.formatMessage({ id: 'data.powerParameters' }),
      dataIndex: 'PowerParams',
      hideInSearch: true,
      width: 100,
    },
  ];
  // 子LC表格
  const lcColumns: ProColumns<any>[] = [
    {
      title: intl.formatMessage({ id: 'data.s/N' }),
      key: 'index',
      valueType: 'index',
      fixed: 'left',
      width: 60,
    },
    {
      title: intl.formatMessage({ id: 'systemManagement.neddTochange.lcType' }),
      dataIndex: 'lcType',
      valueType: 'select',
      initialValue: '1',
      valueEnum: {
        1: {
          text: intl.formatMessage({ id: 'systemManagement.neddTochange.totalLC' }),
        },
        2: {
          text: intl.formatMessage({ id: 'systemManagement.neddTochange.subLC' }),
        },
      },
    },
    {
      title: intl.formatMessage({ id: 'data.acquisitionTime' }),
      dataIndex: 'collectTime',
      key: 'collectTime',
      valueType: 'dateTimeRange',
      initialValue: [dayjs().startOf('day'), dayjs()],
      fixed: 'left',
      width: 100,
      render: (_, record) => {
        return <span>{record.collectTime}</span>;
      },
    },
    {
      title: intl.formatMessage({ id: 'data.reportingTime' }),
      dataIndex: 'reportTime',
      key: 'reportTime',
      valueType: 'dateTimeRange',
      initialValue: [dayjs().startOf('day'), dayjs()],
      fixed: 'left',
      width: 100,
      render: (_, record) => {
        return <span>{record.reportTime}</span>;
      },
    },
    {
      title: intl.formatMessage({ id: 'systemManagement.neddTochange.glcCode' }),
      dataIndex: 'lcCode',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.deviceType' }),
      dataIndex: 'product',
      key: 'product',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.deviceFactory' }),
      dataIndex: 'producer',
      key: 'producer',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.deviceID' }),
      dataIndex: 'devId',
      hideInSearch: true,
      width: 90,
    },
    {
      title: intl.formatMessage({ id: 'data.runningStatus' }),
      dataIndex: 'lcRun',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.gcbStatus' }),
      dataIndex: 'mainBreakerState',
      hideInSearch: true,
      width: 120,
      render: (_, record) => {
        return (
          <span>
            {record.mainBreakerState === 0
              ? intl.formatMessage({ id: 'data.disconnect' })
              : intl.formatMessage({ id: 'data.close' })}
          </span>
        );
      },
    },
    {
      title: intl.formatMessage({ id: 'data.gcbFault' }),
      dataIndex: 'mainBreakerFault',
      hideInSearch: true,
      width: 120,
      render: (_, record) => {
        return (
          <span>
            {record.mainBreakerFault === 1
              ? intl.formatMessage({ id: 'stationRealtimeMonitoring.error' })
              : intl.formatMessage({ id: 'stationRealtimeMonitoring.normal' })}
          </span>
        );
      },
    },
    {
      title: intl.formatMessage({ id: 'data.onlineStatus' }),
      dataIndex: 'onlineState',
      hideInSearch: true,
      width: 100,
      render: (_, record) => {
        return (
          <span>
            {record.lcOnlineState === 1
              ? intl.formatMessage({ id: 'bigdata.online' })
              : intl.formatMessage({ id: 'bigdata.offline' })}
          </span>
        );
      },
    },
    {
      title: intl.formatMessage({ id: 'data.fault' }),
      dataIndex: 'lcFault',
      hideInSearch: true,
      width: 80,
    },
    {
      title: intl.formatMessage({ id: 'data.chargeDischargeStatus' }),
      dataIndex: 'bmsState',
      hideInSearch: true,
      width: 100,
    },
    {
      title: intl.formatMessage({ id: 'data.activePowerKw' }),
      dataIndex: 'activePower',
      hideInSearch: true,
      width: 110,
    },
    {
      title: intl.formatMessage({ id: 'data.maximumPower' }),
      dataIndex: 'ableMaxChgPower',
      hideInSearch: true,
      width: 150,
    },
    {
      title: intl.formatMessage({ id: 'data.maximumDischarge' }),
      dataIndex: 'ableMaxDchgPower',
      hideInSearch: true,
      width: 150,
    },
    {
      title: intl.formatMessage({ id: 'data.powerMode' }),
      dataIndex: 'powerMode',
      hideInSearch: true,
      width: 100,
      render: (_, record) => {
        const text = powerOptions.find((item) => {
          return Number(item.value) === Number(record?.powerMode);
        })?.label;
        return <span>{text}</span>;
      },
    },
    {
      title: intl.formatMessage({ id: 'data.powerParameters' }),
      dataIndex: 'PowerParams',
      hideInSearch: true,
      width: 100,
    },
  ];
  // 切换表格项
  const [sumColumns, setSumColumns] = useState(columns);
  const scroll = {
    x: 'max-content',
  };
  useEffect(() => {
    fetchDictInfo(DATABASE_TYPE_ENUM.POWER_MODE).then((res) => {
      setPowerOptions(res);
    });
  }, []);
  return (
    <>
      <ProTable
        rowKey={'pageIndex'}
        columns={sumColumns}
        scroll={scroll}
        request={async (values) => {
          const params = {
            lcType: values.lcType,
            stationId: initialState?.stationId,
            collectStartTime: values?.collectTime ? values?.collectTime[0] : undefined,
            collectEndTime: values?.collectTime ? values?.collectTime[1] : undefined,
            reportStartTime: values?.reportTime ? values?.reportTime[0] : undefined,
            reportEndTime: values?.reportTime ? values?.reportTime[1] : undefined,
            current: values.current,
            pageSize: values.pageSize,
          };
          if (values.lcType === '1') {
            setSumColumns(columns);
            return await lcControllerGetLcPageDatas(params);
          } else {
            setSumColumns(lcColumns);
            return await lcControllerGetLcPageDatas(params);
          }
        }}
        toolBarRender={() => []}
        customToolBarClass="global-tabs-custom-tools"
      ></ProTable>
    </>
  );
};

export default HistoryData;
